import ZFRequest from './request'
import LocalCache from '@/utils/cache'

const request = new ZFRequest({
  baseURL: process.env.VUE_APP_BASE_URL,
  timeout: Number(process.env.VUE_APP_TIME_OUT),
  interceptors: {
    requestInterceptor(config) {
      const token = LocalCache.getCache('token')
      if (token) {
        config.headers!.Authorization = `Bearer ${token}`
      }
      return config
    },
    requestInterceptorCatch: (err) => {
      console.log('请求失败拦截', err)
      return err
    },
    responseInterceptor: (res) => {
      console.log('响应成功拦截', res)
      return res
    },
    responseInterceptorCatch: (err) => {
      console.log('响应失败拦截', err)
      return err
    }
  }
})
//自定义接口类型：如果项目有多个接口地址时可以配置多个地址和不同的拦截器
/*const request = new ZFRequest({
  baseURL: process.env.VUE_APP_BASE_URL,
  timeout: (<unknown>process.env.VUE_APP_BASE_TIME_OUT) as number,
  interceptors: {
    requestInterceptor: (config) => {
      return config
    },
    requestInterceptorCatch: (err) => {
      return err
    },
    responseInterceptor: (res) => {
      return res
    },
    responseInterceptorCatch: (err) => {
      return err
    }
  }
})*/
export default request
